/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.FeedbackType;
import ses.exceptions.IllegalOrphanException;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.Feedback;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class FeedbackTypeJpaController implements Serializable {

    public FeedbackTypeJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FeedbackType feedbackType) throws RollbackFailureException, Exception {
        if (feedbackType.getFeedbackList() == null) {
            feedbackType.setFeedbackList(new ArrayList<Feedback>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            List<Feedback> attachedFeedbackList = new ArrayList<Feedback>();
            for (Feedback feedbackListFeedbackToAttach : feedbackType.getFeedbackList()) {
                feedbackListFeedbackToAttach = em.getReference(feedbackListFeedbackToAttach.getClass(), feedbackListFeedbackToAttach.getFBId());
                attachedFeedbackList.add(feedbackListFeedbackToAttach);
            }
            feedbackType.setFeedbackList(attachedFeedbackList);
            em.persist(feedbackType);
            for (Feedback feedbackListFeedback : feedbackType.getFeedbackList()) {
                FeedbackType oldFBTypeIdOfFeedbackListFeedback = feedbackListFeedback.getFBTypeId();
                feedbackListFeedback.setFBTypeId(feedbackType);
                feedbackListFeedback = em.merge(feedbackListFeedback);
                if (oldFBTypeIdOfFeedbackListFeedback != null) {
                    oldFBTypeIdOfFeedbackListFeedback.getFeedbackList().remove(feedbackListFeedback);
                    oldFBTypeIdOfFeedbackListFeedback = em.merge(oldFBTypeIdOfFeedbackListFeedback);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FeedbackType feedbackType) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            FeedbackType persistentFeedbackType = em.find(FeedbackType.class, feedbackType.getFBTypeId());
            List<Feedback> feedbackListOld = persistentFeedbackType.getFeedbackList();
            List<Feedback> feedbackListNew = feedbackType.getFeedbackList();
            List<String> illegalOrphanMessages = null;
            for (Feedback feedbackListOldFeedback : feedbackListOld) {
                if (!feedbackListNew.contains(feedbackListOldFeedback)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Feedback " + feedbackListOldFeedback + " since its FBTypeId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Feedback> attachedFeedbackListNew = new ArrayList<Feedback>();
            for (Feedback feedbackListNewFeedbackToAttach : feedbackListNew) {
                feedbackListNewFeedbackToAttach = em.getReference(feedbackListNewFeedbackToAttach.getClass(), feedbackListNewFeedbackToAttach.getFBId());
                attachedFeedbackListNew.add(feedbackListNewFeedbackToAttach);
            }
            feedbackListNew = attachedFeedbackListNew;
            feedbackType.setFeedbackList(feedbackListNew);
            feedbackType = em.merge(feedbackType);
            for (Feedback feedbackListNewFeedback : feedbackListNew) {
                if (!feedbackListOld.contains(feedbackListNewFeedback)) {
                    FeedbackType oldFBTypeIdOfFeedbackListNewFeedback = feedbackListNewFeedback.getFBTypeId();
                    feedbackListNewFeedback.setFBTypeId(feedbackType);
                    feedbackListNewFeedback = em.merge(feedbackListNewFeedback);
                    if (oldFBTypeIdOfFeedbackListNewFeedback != null && !oldFBTypeIdOfFeedbackListNewFeedback.equals(feedbackType)) {
                        oldFBTypeIdOfFeedbackListNewFeedback.getFeedbackList().remove(feedbackListNewFeedback);
                        oldFBTypeIdOfFeedbackListNewFeedback = em.merge(oldFBTypeIdOfFeedbackListNewFeedback);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = feedbackType.getFBTypeId();
                if (findFeedbackType(id) == null) {
                    throw new NonexistentEntityException("The feedbackType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            FeedbackType feedbackType;
            try {
                feedbackType = em.getReference(FeedbackType.class, id);
                feedbackType.getFBTypeId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The feedbackType with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Feedback> feedbackListOrphanCheck = feedbackType.getFeedbackList();
            for (Feedback feedbackListOrphanCheckFeedback : feedbackListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This FeedbackType (" + feedbackType + ") cannot be destroyed since the Feedback " + feedbackListOrphanCheckFeedback + " in its feedbackList field has a non-nullable FBTypeId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(feedbackType);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FeedbackType> findFeedbackTypeEntities() {
        return findFeedbackTypeEntities(true, -1, -1);
    }

    public List<FeedbackType> findFeedbackTypeEntities(int maxResults, int firstResult) {
        return findFeedbackTypeEntities(false, maxResults, firstResult);
    }

    private List<FeedbackType> findFeedbackTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from FeedbackType as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FeedbackType findFeedbackType(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FeedbackType.class, id);
        } finally {
            em.close();
        }
    }

    public int getFeedbackTypeCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from FeedbackType as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
